package cn.sdormitory.index.service.impl;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.vo.BClassVo;
import cn.sdormitory.index.dao.IndexDataDao;
import cn.sdormitory.index.service.IndexDataService;
import cn.sdormitory.school.entity.SchoolDuty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexDataServiceImpl implements IndexDataService {

    @Autowired
    IndexDataDao indexDataDao;

    @Autowired
    BClassDao bClassDao;


    @Override
    public List<Map<String,Object>> findLastDaySdAttenceData() {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = sdf.format(d);
        return indexDataDao.findLastDaySdAttenceData(lastDay);
    }

    @Override
    public List<Map<String, Object>> findClassWeekLeaveData() {
        List<Map<String, Object>> allClassWeekLeaveData = new ArrayList<>();
        //查询所有班级
        List<BClassVo> allClass = bClassDao.getAllAppClassList();
        for(BClassVo item : allClass){
            //一个班级的数据
            Map<String, Object> classWeekLeaveData = new HashMap<>();
            //折线图标题
            classWeekLeaveData.put("brokenTitle",item.getClassName());
            //折线图X轴
            List<Object> brokenX = new LinkedList<>();
            //折线图Y轴
            List<Object> brokenY = new LinkedList<>();
            List<Map<String, Object>> leaveCountData = indexDataDao.findNearWeekLeaveDataByClassId(item.getClassId());
            for (int i = 0; i < leaveCountData.size(); i++) {
                Map<String, Object> map = leaveCountData.get(i);
                brokenX.add(map.get("nearWeek"));
                brokenY.add(map.get("leaveCount"));
            }
            classWeekLeaveData.put("brokenX",brokenX);
            classWeekLeaveData.put("brokenY",brokenY);
            allClassWeekLeaveData.add(classWeekLeaveData);
        }
        return allClassWeekLeaveData;
    }

    @Override
    public List<Map<String, Object>> findTodayClassAttenceData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        return indexDataDao.findTodayClassAttenceData(today);
    }

    @Override
    public Map<String, Object> findSdRepairCount() {
        return indexDataDao.findSdRepairCount();
    }

    @Override
    public Map<String, Object> findClassRepairCount() {
        return indexDataDao.findClassRepairCount();
    }

    @Override
    public List<SchoolDuty> findTodayDutyPerson() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        return indexDataDao.findTodayDutyPerson(today);
    }

    @Override
    public List<Map<String, Object>> findDvCount() {
        List<Map<String, Object>> dvCount = indexDataDao.findDvCount();

//        5 11 18
//        for (int i = 0; i < dvCount.size(); i++) {
//
//            if("1".equals(dvCount.get(i).get("dvType"))){
//
//            }
//        }
//        for(Map<String, Object> map : dvCount){
//            if("1".equals(dvCount.get()))
//        }
        return indexDataDao.findDvCount();
    }
}
